package br.cin.ufpe.in1118.middleware.distribution.services;

import br.cin.ufpe.in1118.middleware.communication.TCPAddress;
import br.cin.ufpe.in1118.middleware.distribution.Middleware;
import br.cin.ufpe.in1118.middleware.exceptions.RemoteException;

public abstract class GenericStub {
	
	private ObjectReference objectReference;
	private Middleware middleware;

	public GenericStub(ObjectReference objectReference) {
		this.objectReference = objectReference;
		this.middleware = new Middleware(new TCPAddress(this.objectReference.getHost(), this.objectReference.getPort()));	
	}

	protected Object execute(String operationName, Object... parameters) throws RemoteException {
		Object ret = this.middleware.invoke(this.objectReference.getObjectId(), operationName, parameters);
		if (ret instanceof Exception) {
			throw new RemoteException((Exception) ret);
		}
		return ret;
	}

}
